package com.solution.admin.auth.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.solution.admin.auth.entity.RoleEntity;
import com.solution.admin.auth.service.RoleService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.LayuiConstants;
import com.solution.common.page.LayuiJSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;

/**
 * 角色管理控制层
 * 
 * @author yaoyu
 * @date 2017年3月28日 下午6:51:29
 */
@RequestMapping("/role")
@Controller
public class RoleController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(RoleController.class);

	/**
	 * 角色管理业务dubbo接口
	 */
	@Autowired
	private RoleService roleService;

	/**
	 * 去查询角色列表
	 * @author james
	 */
	@RequestMapping("/toRoleList")
	public String toRoleList(RoleEntity role, HttpServletRequest request) {
		return "auth/role_list";
	}

	/**
	 * 查询角色列表
	 * 
	 * @author yaoyu
	 * @date 2017年3月29日 下午2:31:33
	 * @param request
	 * @return
	 */
	@RequestMapping("/listRolePage")
	@ResponseBody
	public LayuiJSONGrid listRolePage(RoleEntity role, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("limit").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		// 封装参数
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("roleName", role.getRoleName());
		param.put("status", role.getStatus());
		param.put("isDelete", "1");
		try {
			PageBean pageBean = roleService.listPage(pageParam, param);
			return new LayuiJSONGrid(LayuiConstants.SUCCESS,LayuiConstants.SUC_MSG,pageBean.getTotalCount(), pageBean.getRecordList());
		} catch (Exception e) {
			handleException("角色分页查询", logger, e);
		}
		return new LayuiJSONGrid();
	}
	
	
	/**
	 * 查询角色列表(for 异步加载下拉框数据)
	 * @author james 
	 */
	@RequestMapping("/listRole")
	@ResponseBody
	public List<RoleEntity> listRole(HttpServletRequest request){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("status", "1");
		List<RoleEntity> roleList = roleService.listAllRoleList();
		return roleList;
	}
	
	
	/**
	 * 新增/修改角色
	 * @author james
	 */
	@RequestMapping("/saveOrUpdateRole")
	@ResponseBody
	public Map<String, String> saveOrUpdateRole(RoleEntity role, HttpServletRequest request) {
		Map<String, String> resultMap = new HashMap<String, String>();
		String resourceIds = request.getParameter("resourceIds");
		int result = 0;
		try {
			if(role.getId() != null){
				role.setModifier(getUserCode());
				role.setModifyTime(new Date());
				long roleId = roleService.saveRole(role, resourceIds);
				result = roleId > 0 ? 1 : 0;
			}else{
				role.setCreater(getUserCode());
				role.setCreateTime(new Date());
				result = roleService.updateRole(role, resourceIds);
			}
			syncSuccessReslt(resultMap, result);
		} catch (Exception e) {
			handleException("添加角色异常", logger, e);
			syncErrorReslt(resultMap);
		}
		return resultMap;
	}

	/**
	 * 跳转修改
	 * 
	 * @param request
	 * @return
	 * @author zhuoyu
	 * @date 2017年4月21日 下午2:46:36
	 */
	@RequestMapping("/toUpdatePage")
	public ModelAndView toEditPage(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String roleCode = request.getParameter("id");
		RoleEntity role = roleService.getRoleById(Long.valueOf(roleCode));
		mav.setViewName("auth/role/role_edit");
		mav.addObject("role", role);
		return mav;
	}

	/**
	 * 去添加
	 * 
	 * @author yaoyu
	 * @date 2017年3月28日 下午5:37:35
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddPage")
	public ModelAndView toAddPage(RoleEntity role, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("auth/role/role_add");
		return mav;
	}
	/**
	 * 跳转到更新页面
	 * 
	 * @author yaoyu
	 * @date 2017年3月29日 下午2:59:23
	 * @param request
	 * @return
	 */
	@RequestMapping("/toUpdateRolePage")
	@ResponseBody
	public ModelAndView toUpdateRolePage(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String roleCode = request.getParameter("roleCode");
		try {
			RoleEntity role = roleService.getById(Long.parseLong(roleCode));
			if (role != null) {
				mav.addObject("role", role);
			} else {
				mav.addObject("role", new RoleEntity());
			}
		} catch (Exception e) {
			logger.error("系统异常===", e);
			e.printStackTrace();
		}
		mav.setViewName("auth/role/role_update");
		return mav;
	}
	
	/**
	 * 启用角色
	 * @author james
	 */
	@RequestMapping("/enableRole")
	@ResponseBody
	public Map<String, String> enableRole(RoleEntity role, HttpServletRequest request) {
		Map<String, String> resultMap = new HashMap<String, String>();
		try {
			role.setStatus(1);
			int result = roleService.updateRoleStatus(role);
			syncSuccessReslt(resultMap, result);
		} catch (Exception e) {
			handleException("启用角色异常", logger, e);
			syncErrorReslt(resultMap);
		}
		return resultMap;
	}
	
	/**
	 * 停用角色
	 * @author james
	 */
	@RequestMapping("/disableRole")
	@ResponseBody
	public Map<String, String> disableRole(RoleEntity role, HttpServletRequest request) {
		Map<String, String> resultMap = new HashMap<String, String>();
		try {
			role.setStatus(0);
			int result = roleService.updateRoleStatus(role);
			syncSuccessReslt(resultMap, result);
		} catch (Exception e) {
			handleException("停用角色异常", logger, e);
			syncErrorReslt(resultMap);
		}
		return resultMap;
	}

	/**
	 * 验证角色名编号是否存在
	 * @return
	 * @author zhuoyu
	 * @date 2017年4月13日 上午11:14:08
	 * @modify <scy> <2017年4月13日 上午11:14:08>
	 */
	@RequestMapping("/getRoleByNameOrCode")
	@ResponseBody
	public Long getRoleByNameOrCode(HttpServletRequest request) {
		String roleCode = request.getParameter("roleCode");
		String roleName = request.getParameter("roleName");
		Long count = roleService.getRoleByNameOrCode(roleName, roleCode);
		return count;
	}

	/**
	 * 查询所有角色信息
	 * 
	 * @return
	 * @exception/throws [异常类型][异常说明]
	 * @author diwankang
	 * @date 2017年4月14日 上午8:58:22
	 * @modify <scy> <2017年4月14日 上午8:58:22>
	 */
	@RequestMapping("/listAllRole")
	@ResponseBody
	public Map<String, Object> listAllRole() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<RoleEntity> roleList = roleService.listAllRoleList();
			resultMap.put("roleList", roleList);
		} catch (Exception e) {
			logger.error("系统异常===", e);
			e.printStackTrace();
		}
		return resultMap;
	}
}
